home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 151-175 / disk_162 / setpalorntsc / tdemo.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  8KB  |  269 lines

  1. /* Amiga Demo program */
  2. /* Shows fast area filling (triangles) in C plus color palette animation */
  3. /* P. Kittel CBM Ffm 23 Jul 87, 13 Dec 87 */
  4. /* Version with automatic NTSC/PAL recognization */
  5. /* When called from CLI a number can be added to limit run time,
  6.    1 count is approx. 1/2 second */
  7. /* Compiled with Lattice C Compiler 3.10 */
  8. /* Link: */
  9. /* .Key f 
  10.    Blink FROM LIB:c.o+<f>.o TO <f> LIB
  11.                                    LIB:lc.lib+LIB:amiga.lib+LIB:lcm.lib */
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <intuition/intuition.h>
  16. #include <graphics/gfxmacros.h>
  17. #include <graphics/gfxbase.h>
  18. #include <graphics/text.h>
  19. #include <math.h>
  20. struct IntuitionBase *IntuitionBase; /* Pointer aus exec */
  21. struct GfxBase *GfxBase;
  22. struct ViewPort *vp;
  23. #define INTUITION_REV 33             /* Version 33 */
  24. #define GRAPHICS_REV 33
  25.  
  26. struct TextFont *font;
  27. struct TextAttr MyFont =
  28.    {
  29.    "topaz.font",
  30.    TOPAZ_SIXTY,
  31.    FS_NORMAL,
  32.    FPF_ROMFONT,
  33.    };
  34.  
  35. struct NewScreen NewScreen =
  36.    {
  37.    0,
  38.    0,
  39.    570,
  40.    256,
  41.    4,
  42.    0, 1,
  43.    HIRES,
  44.    CUSTOMSCREEN,
  45.    &MyFont,
  46.    "Dreier",
  47.    NULL,
  48.    NULL,
  49.    };
  50.  
  51. void main(argc,argv)
  52. int argc;
  53. char *argv[];
  54. {
  55.    struct Screen *Screen;
  56.    struct NewWindow NewWindow;
  57.    struct Window *Window;
  58.    struct RastPort *rp;
  59.    UWORD areabuffer[20];
  60.    struct TmpRas mytmpras;
  61.    struct AreaInfo myareainfo;
  62.    PLANEPTR myplane;
  63.    struct IntuiMessage *msg;
  64.    int i,x1,y1,x2,y2,x3,y3,x9,y9,nn,nf,pf,ffi,co,fpal,z,zd,zm;
  65.    int cc,c2,c6,cd;
  66.    double fs,ff,sif,y0,ye,z1,z2,s2,z3,a,m,fr,fg,fb,ar,ag,ab,cf,nn1;
  67.    double pr[16],pg[16],pb[16];
  68.    int    pra[17],pga[17],pba[17];
  69.  
  70.    nf=4;     nn=15;
  71.  
  72.    nn1=(double)(nn-1);
  73.  
  74.    x9=500;   y9=184+53+2; fpal=1;   /* PAL */
  75.              y0= 58.0;    ye=119.0;
  76.    x1=250;   y1=140;      z1=0.0;
  77.    x2= 90;   y2=y1;       z2=0.0;   z3=0.0;
  78.  
  79.    fs=0.0; ff=4000.0; sif=273.0+sin(0.15);  pf=0;
  80.    c2=15*256; c6=15*16; co=2;
  81.    cf=1.0; cc=0;        fr=0.0; fg=0.0; fb=0.0;
  82.  
  83.    IntuitionBase = (struct IntuitionBase *)
  84.       OpenLibrary ("intuition.library",INTUITION_REV);
  85.    if (IntuitionBase == NULL)
  86.       { fpal=0;
  87.         IntuitionBase = (struct IntutionBase *)
  88.            OpenLibrary ("intuition.library",0);
  89.         if (IntuitionBase == NULL) exit (FALSE);
  90.       }
  91.  
  92.    GfxBase = (struct GfxBase *)
  93.       OpenLibrary("graphics.library",GRAPHICS_REV);
  94.    if(GfxBase == NULL)
  95.       { CloseLibrary(IntuitionBase);
  96.         exit (FALSE);
  97.       }
  98.  
  99.    /* PAL / NTSC  ? */
  100.    if (fpal > 0)
  101.       { if (!((GfxBase->DisplayFlags) & PAL)) fpal=0; }
  102.  
  103.    if (fpal == 0)  
  104.       { NewScreen.Height=200; /* NTSC */
  105.         y9-=55; y1=105; y0=45.0; ye=93.0;
  106.       }
  107.  
  108.    if((Screen = (struct Screen *) OpenScreen (&NewScreen)) == NULL)
  109.       { CloseLibrary(GfxBase);
  110.         CloseLibrary(IntuitionBase);
  111.         exit (FALSE);
  112.       }
  113.  
  114.    NewWindow.LeftEdge = 0;
  115.    NewWindow.TopEdge  = 0;
  116.    NewWindow.Width  = x9+9;
  117.    NewWindow.Height = y9+16;
  118.    NewWindow.DetailPen = 0;
  119.    NewWindow.BlockPen  = 1;
  120.    NewWindow.Title = "Commodore - AMIGA";
  121.    NewWindow.Flags =
  122.      WINDOWCLOSE|GIMMEZEROZERO|ACTIVATE;
  123.    NewWindow.IDCMPFlags = CLOSEWINDOW;
  124.    NewWindow.Type = CUSTOMSCREEN;
  125.    NewWindow.FirstGadget = NULL;
  126.    NewWindow.CheckMark = NULL;
  127.    NewWindow.Screen = Screen;
  128.    NewWindow.BitMap = NULL;
  129.    NewWindow.MinWidth = x9;
  130.    NewWindow.MinHeight = y9;
  131.    NewWindow.MaxWidth = x9;
  132.    NewWindow.MaxHeight = y9;
  133.    if(( Window = ( struct Window *) OpenWindow( &NewWindow )) == NULL )
  134.      { CloseScreen (Screen);
  135.        CloseLibrary(IntuitionBase);
  136.        CloseLibrary(GfxBase);
  137.        exit(FALSE);
  138.      }
  139.  
  140.    rp=Window->RPort;
  141.    vp=&Screen->ViewPort;
  142.    font=OpenFont(&MyFont);
  143.    SetFont(rp,font);
  144.  
  145.    for (i=2; i<=nn; i++)
  146.      { pra[i]=0; pga[i]=0; pba[i]=0;
  147.        SetRGB4(vp,i,0,0,0);
  148.      }
  149.    SetRGB4(vp,0, 7, 7, 7);
  150.    SetRGB4(vp,1,15,15,15);
  151.  
  152.    InitArea(&myareainfo,areabuffer,8);
  153.    rp->AreaInfo=&myareainfo;
  154.    myplane     =AllocRaster(320,256);
  155.    rp->TmpRas  =InitTmpRas(&mytmpras,myplane,RASSIZE(320,256));
  156.  
  157.    SetAPen(rp,1);
  158.    Move(rp,       4,    8); Text(rp,"Superfast"    , 9);
  159.    Move(rp,       4,   17); Text(rp,"graphics"     , 8);
  160.    SetAPen(rp,nn);
  161.    Move(rp,       4,y9-12); Text(rp,"4096"         , 4);
  162.    Move(rp,       4,y9- 3); Text(rp,"colors"       , 6);
  163.    SetAPen(rp,1);
  164.    Move(rp,x9-12*10,    8); Text(rp,"Area filling" ,12);
  165.    Move(rp,x9-10*10,   17); Text(rp,"by Blitter"   ,10);
  166.    Move(rp,x9- 6*10,y9-12); Text(rp,"Please"       , 6);
  167.    Move(rp,x9- 8*10,y9- 3); Text(rp,"compare!"     , 8);
  168.  
  169.    zm = 9;
  170.    zd = 0;
  171.    if (argc > 1) { zm=atoi(argv[argc-1]); zd=1; } /* Time limit */
  172.    z  = 0;
  173.  
  174.    while (z < zm)
  175.      {
  176.        if (msg = (struct IntuiMessage *)GetMsg(Window->UserPort))
  177.          { ReplyMsg(msg); z=zm+1; }
  178.  
  179.        /* Compute new triangle coordinate, */
  180.        /* radius and propagation angle are sine modulated */
  181.        z1+=0.01;           if (z1 > 6.28) z1-=6.28;
  182.        z2+=0.03*sin(z1);   if (z2 > 6.28) z2-=6.28;
  183.        s2 =sin(z2);
  184.        z3+=z1*s2/4;        if (z3 > 6.28) z3-=6.28;
  185.  
  186.        x3=x2;
  187.        y3=y2;
  188.        x2=(int)(120.0*(1.0+s2)*cos(z3)+(double)x1);
  189.        y2=(int)(   y0*(1.0+s2)*sin(z3)+        ye);
  190.  
  191.        co+=1;
  192.        if (co > nn)
  193.          { co=2; z+=zd;
  194.  
  195.            /* Compute new color value, */
  196.            /* propagation distance in values is sine modulated, */
  197.            /* value of sif=1+16+256+(some odd decimals) */
  198.            /* shall increase every color component by 1 */
  199.            fs+=0.1; if (fs > 7.0) fs-=7.0;
  200.            ff+=sif*(1.0+cos(fs)*1.02);
  201.            if (ff > 4095.0) ff-=4095.0;
  202.            ffi=(int)ff;
  203.            ar=fr; ag=fg; ab=fb;
  204.            fr=(ffi & 15)/15.0;
  205.            fg=(ffi & c6)/((double)c6);
  206.            fb=(ffi & c2)/((double)c2);
  207.            pr[nn]=fr; pg[nn]=fg; pb[nn]=fb;
  208.  
  209.            if (cc++ > 20) { cc=0; cf=-cf; }
  210.  
  211.            /* Interpolate color values */
  212.            /* Depending on cf, wrap always around 0 or 1, respectively, */
  213.            /* to provide colors not being too identical */
  214.            if ((cf*(fr+ar)) > cf)
  215.              { m=(2.0-fr-ar)/nn1; a=ar-m;
  216.                for (i=2; i<nn; i++)
  217.                  { pr[i]=a+((double)i)*m; if (pr[i] > 1.0) pr[i]=2.0-pr[i]; }
  218.              } else
  219.              { m=(   -fr-ar)/nn1; a=ar-m;
  220.                for (i=2; i<nn; i++)
  221.                  { pr[i]=a+((double)i)*m; if (pr[i] < 0.0) pr[i]=   -pr[i]; }
  222.              }
  223.  
  224.            if ((cf*(fg+ag)) > cf)
  225.              { m=(2.0-fg-ag)/nn1; a=ag-m;
  226.                for (i=2; i<nn; i++)
  227.                  { pg[i]=a+((double)i)*m; if (pg[i] > 1.0) pg[i]=2.0-pg[i]; }
  228.              } else
  229.              { m=(   -fg-ag)/nn1; a=ag-m;
  230.                for (i=2; i<nn; i++)
  231.                  { pg[i]=a+((double)i)*m; if (pg[i] < 0.0) pg[i]=   -pg[i]; }
  232.              }
  233.               
  234.            if ((cf*(fb+ab)) > cf)
  235.              { m=(2.0-fb-ab)/nn1; a=ab-m;
  236.                for (i=2; i<nn; i++)
  237.                  { pb[i]=a+((double)i)*m; if (pb[i] > 1.0) pb[i]=2.0-pb[i]; }
  238.              } else
  239.              { m=(   -fb-ab)/nn1; a=ab-m;
  240.                for (i=2; i<nn; i++)
  241.                  { pb[i]=a+((double)i)*m; if (pb[i] < 0.0) pb[i]=   -pb[i]; }
  242.              }
  243.          }
  244.  
  245.        /* Cycle Palette */
  246.        for (i=nn+1; i>2; i--)
  247.          { pra[i]=pra[i-1]; pga[i]=pga[i-1];  pba[i]=pba[i-1]; }
  248.        pra[2]=pra[nn+1];    pga[2]=pga[nn+1]; pba[2]=pba[nn+1];
  249.        cd=2*co-2; if (cd > nn) cd=cd-nn+1;
  250.        pra[cd]=(int)(pr[co]*nn);
  251.        pga[cd]=(int)(pg[co]*nn);
  252.        pba[cd]=(int)(pb[co]*nn);
  253.        for (i=2; i<=nn; i++) SetRGB4(vp,i,pra[i],pga[i],pba[i]);
  254.  
  255.        /* Draw triangle */
  256.        SetAPen (rp,co);
  257.        AreaMove(rp,x1,y1);
  258.        AreaDraw(rp,x2,y2);
  259.        AreaDraw(rp,x3,y3);
  260.        AreaEnd (rp);
  261.      }  
  262.  
  263.    FreeRaster  (myplane,320,256);
  264.    CloseWindow (Window);
  265.    CloseScreen (Screen);
  266.    CloseLibrary(IntuitionBase);
  267.    CloseLibrary(GfxBase);
  268. }
  269.